#!/usr/bin/env bash
dir_root=/app
APP_URL=https://gitee.com/523757325/torrent-manager-node.git
APP_BRANCH=master
WEB_URL=https://gitee.com/523757325/torrent-manager-web.git
WEB_BRANCH=master

## 克隆脚本，$1：仓库地址，$2：仓库保存路径，$3：分支（可省略）
git_clone_scripts() {
    local url=$1
    local dir=$2
    local branch=$3
    [[ $branch ]] && local cmd="-b $branch "
    echo -e "开始克隆仓库 $url 到 $dir\n"
    git clone $cmd $url $dir
    exit_status=$?
}

## 更新脚本，$1：仓库保存路径
git_pull_scripts() {
    local dir_current=$(pwd)
    local dir_work="$1"
    local branch="$2"
    [[ $branch ]] && local cmd="origin/${branch}"
    cd $dir_work
    echo -e "开始更新仓库：$dir_work\n"
    git fetch --all
    exit_status=$?
    git reset --hard $cmd
    git pull
    cd $dir_current
}

## 重置仓库remote url，docker专用，$1：要重置的目录，$2：要重置为的网址
reset_romote_url() {
    local dir_current=$(pwd)
    local dir_work=$1
    local url=$2
    local branch="$3"

    [[ $branch ]] && local cmd="origin/${branch}"

    if [ -d "$dir_work/.git" ]; then
        cd $dir_work
        git remote set-url origin $url >/dev/null
        git reset --hard $cmd >/dev/null
        cd $dir_current
    fi
}

## npm install 子程序
npm_install_sub() {
    npm install --registry=https://registry.npm.taobao.org || npm install
}

## npm install，$1：package.json文件所在路径
npm_install_1() {
    local dir_current=$(pwd)
    local dir_work=$1

    cd $dir_work
    echo -e "运行 npm install...\n"
    npm_install_sub
    [[ $? -ne 0 ]] && echo -e "\nnpm install 运行不成功，请进入 $dir_work 目录后手动运行 npm install...\n"
    cd $dir_current
}

npm_install_2() {
    local dir_current=$(pwd)
    local dir_work=$1

    cd $dir_work
    echo -e "检测到 $dir_work 的依赖包有变化，运行 npm install...\n"
    npm_install_sub
    if [[ $? -ne 0 ]]; then
        echo -e "\n安装 $dir_work 的依赖包运行不成功，再次尝试一遍...\n"
        npm_install_1 $dir_work
    fi
    cd $dir_current
}

npm_run_build() {
    local dir_current=$(pwd)
    local dir_work=$1

    cd $dir_work
    echo -e "开始在 $dir_work 进行页面构建，运行 npm run build...\n"
    npm run build
    cd $dir_current
}

## 重启子程序
restart() {
    echo -e "重启程序中..."
    sleep 1
    pm2 restart torrentmanager
}

## 更新子程序
update() {
    echo -e "--------------------------更新程序 开始------------------------------------\n"
    [ -f /web/package.json ] && web_depend_old=$(cat /web/package.json)
    reset_romote_url /web ${WEB_URL} ${WEB_BRANCH}
    git_pull_scripts /web ${APP_BRANCH}
    if [[ $exit_status -eq 0 ]]; then
        echo -e "\n更新/web成功...\n"
        [ -f /web/package.json ] && web_depend_new=$(cat /web/package.json)
        [[ "$web_depend_old" != "$web_depend_new" ]] && npm_install_2 /web
        npm_run_build /web
    else
        echo -e "\n更新/web失败，请检查原因...\n"
    fi

    [ -f $dir_root/package.json ] && app_depend_old=$(cat $dir_root/package.json)
    reset_romote_url ${dir_root} ${APP_URL} ${APP_BRANCH}
    git_pull_scripts $dir_root ${APP_BRANCH}
    if [[ $exit_status -eq 0 ]]; then
        echo -e "\n更新$dir_root成功...\n"
        [ -f $dir_root/package.json ] && app_depend_new=$(cat $dir_root/package.json)
        [[ "$app_depend_old" != "$app_depend_new" ]] && npm_install_2 $dir_root
    else
        echo -e "\n更新$dir_root失败，请检查原因...\n"
    fi
    echo -e "--------------------------更新程序 结束------------------------------------\n"
}

## 更新程序
main() {
    local param="$1"
    if [[ $param = "restart" ]]; then
        restart
    else
        update
    fi
}

main "$@"

exit 0
